<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI Web Develop
 *
 * @package     HITI Web Dev
 * @category    Models
 * @see 		Product
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Product_model extends CI_Model {
	public function __contruct()
	{
		parent::__contruct();
	}

	public function show($limit = 12, $get_row = false, $cat_id = '', $q = '', $type = '')
	{
		$this->db->query('SET NAMES "LATIN1"');
		$lng = $GLOBALS['cfg']['lng'];
		if ($cat_id)
		{
			$cats = array();
			$cats[] = $cat_id;
			$this->db->select('id');
			$this->db->where('parent', $cat_id);
			$query = $this->db->get('product_categories');
			foreach ($query->result_array() as $data)
			{
				$cats[] = $data['id'];
				$this->db->select('id');
				$this->db->where('parent', $data['id']);
				$query = $this->db->get('product_categories');
				foreach ($query->result_array() as $data2)
				{
					$cats[] = $data2['id'];
					$this->db->select('id');
					$this->db->where('parent', $data2['id']);
					$query = $this->db->get('product_categories');
					foreach ($query->result_array() as $data3)
					{
						$cats[] = $data3['id'];
					}
				}
			}
			$this->db->where_in('cat', $cats);
		}
		$this->db->select('id');
		if ($q)
		{
			if (is_array($q) && count($q))
			{
				$this->db->where_in('id', $q);
			}
			else
			{
				$this->db->where(sprintf('(name_vn LIKE "%%%s%%" OR name_en LIKE "%%%s%%" OR keyword LIKE "%%%s%%")', $q, $q, str_replace(' ', '-', $q)));
			}
		}
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		if ($type)
		{
			$this->db->where($type, 1);
		}
		if (!$get_row)
		{
			$this->db->select('keyword, code, name_'.$lng.' AS name, des_'.$lng.' AS des, alt_'.$lng.' AS alt, img, gia1, gia2, pricelist, (SELECT keyword FROM product_categories WHERE id = CONCAT("", cat, "")) AS catkey, bh', false);
			if ($type == 'new')
            {
                $this->db->order_by('order_new '.$GLOBALS['var']['order_mode']);
            }
            else
            {
                $this->db->order_by($GLOBALS['var']['order_by'].' '.$GLOBALS['var']['order_mode']);
            }
			if ($type)
			{
				$this->db->limit($limit);
			}
			else if ($GLOBALS['var']['rowstart'])
			{
				$this->db->limit($limit, $GLOBALS['var']['rowstart'] * $limit - $limit);
			}
		}
		$query = $this->db->get('products');
		if ($get_row) return $query->num_rows();
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		return false;
	}

	public function info($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$lng = $GLOBALS['cfg']['lng'];
		$this->db->from('products AS prod');
		$this->db->select('prod.*, prod.name_'.$lng.' AS name, prod.alt_'.$lng.' AS alt, prod.des_'.$lng.' AS des, prod.info_'.$lng.' AS info, prop1.name_'.$lng.' AS xuatxu, prop2.name_'.$lng.' AS bh, prop3.name_'.$lng.' AS tinhtrang, (SELECT m.name_'.$lng.' FROM trademarks m WHERE m.id = nsx) AS nsx');
		$this->db->where('prod.active', 1);
		$this->db->where('prod.deleted', 0);
		if (is_numeric($id))
		{
			$this->db->where('prod.id', $id);
		}
		else
		{
			$this->db->where('prod.keyword', $id);
		}
		$this->db->join('product_properties AS prop1', 'prop1.id = prod.xuatxu', 'left');
		$this->db->join('product_properties AS prop2', 'prop2.id = prod.bh', 'left');
		$this->db->join('product_properties AS prop3', 'prop3.id = prod.tinhtrang', 'left');
		$query = $this->db->get();
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			return false;
		}
	}

	public function category($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$lng = $GLOBALS['cfg']['lng'];
		$this->db->where('id', $id);
		$this->db->select('id, name_'.$lng.' AS name');
		$query = $this->db->get('product_categories');
		if ($query->num_rows())
		{
			return $query->row_array();
		}
		else
		{
			return false;
		}
	}

	public function list_conmnents($product, $reply = 0)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$this->db->select('id, user, name, email, info, date_added');
		$this->db->where('reply', $reply);
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		$this->db->where('product', $product);
		$this->db->order_by('id desc');
		$query = $this->db->get('comments');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function list_article($product)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$lng = $GLOBALS['cfg']['lng'];
		$this->db->select('id, keyword, name_'.$lng.' AS name, img');
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		$this->db->where_in('id', $product);
		$query = $this->db->get('articles');
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
		else
		{
			return false;
		}
	}

	public function cat($id)
	{
		$this->db->query('SET NAMES "LATIN1"');
		$lng = $GLOBALS['cfg']['lng'];
		$this->db->select('id, name_'.$lng.' AS name, keyword');
		$this->db->where('active', 1);
		$this->db->where('deleted', 0);
		if (is_numeric($id))
		{
			$this->db->where('id', $id);
		}
		else
		{
			$this->db->where('keyword', $id);
		}
		$query = $this->db->get('product_categories');
		if ($query->num_rows() > 0)
		{
			return $query->row_array();
		}
		else
		{
			return false;
		}
	}

	public function upview($id)
	{
		$this->db->query('UPDATE products SET view = view + 1 WHERE id = "'.$id.'"', true);
	}

}

/* End of file product_model.php */
/* Location: ./application/models/product_model.php */